package jejunu.androboys.QuickCall;

import jejunu.androboys.QuickCall.Component.FileController;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class SettingModeAcrivity extends Activity implements OnClickListener
{
	ListView lv;
	RadioGroup mode_group;
	
	String[][] mSave_data;
	Button bt_save;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	
	    FileController fc = new FileController(this);
	    mSave_data=fc.loadListFile();
	    setContentView(R.layout.setting_mode);
	    
        lv=(ListView) findViewById(R.id.Setting_listV_list);
        mode_group=(RadioGroup) findViewById(R.id.Setting_rGroup_mode);
        
        
        
        bt_save=(Button) findViewById(R.id.Setting_bt_save);
        bt_save.setOnClickListener(this);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode==1234)
		{
			if(resultCode==RESULT_OK)
			{
				int position=data.getIntExtra("sel_item", 0);
				
				mSave_data[position][0]=data.getStringExtra("name");
				mSave_data[position][1]=data.getStringExtra("number");
				mSave_data[position][2]=data.getStringExtra("type");
				
				Log.v("onActResult", data.getStringExtra("name").toString()+"");
				Log.v("onActResult", data.getStringExtra("number").toString()+"");
				Log.v("onActResult", data.getStringExtra("type").toString()+"");
				Log.v("onActResult", data.getIntExtra("sel_item", 100)+"");
				
				for(int i=0; i<mSave_data.length; i++)
				{
					Log.v("mText", mSave_data[i][0]+"");
				}
			}
		}
	}
	
	@Override
	protected void onResume() 
	{
		super.onResume();
		
		SettingListAdapter story_list=new SettingListAdapter(this, R.layout.setting_contents, mSave_data);
        
        lv.setAdapter(story_list);
		Log.v("onresume", "resume");
		
		setCheckedRadioBt();
	}

	@Override
	public void onClick(View v) 
	{
		if(v==bt_save)
		{
			Log.v("savebt", "start");
			
			FileController fc=new FileController(this);
			fc.saveListFile(mSave_data);
			
			saveSelectedMode();
			
			Toast.makeText(this, "설정이 저장 되었습니다!", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void setCheckedRadioBt()
	{
		FileController fc=new FileController(this);
		int mode=fc.loadModeFile();
		mode_group.check(mode_group.getChildAt(mode).getId());
	}
	
	public void saveSelectedMode()
	{
		RadioButton checked_bt=(RadioButton) findViewById(mode_group.getCheckedRadioButtonId());
		
		int sel_mode=Integer.parseInt((String) checked_bt.getTag());
		
		FileController fc=new FileController(this);
		fc.saveModeFile(sel_mode);
		
		MainTabActivity a = (MainTabActivity) getParent();
		a.setsetCurrentTab(sel_mode);
	}
}



class SettingListAdapter extends BaseAdapter
{
	Activity mAct;
	int mListLayout;
	LayoutInflater inflater;
	String[][] mSave_data;
	
	public SettingListAdapter(Activity act, int listLayout, String[][] save_data) 
	{
		mAct=act;
		mListLayout=listLayout;
		inflater = (LayoutInflater) mAct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mSave_data=save_data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 8;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		if(convertView==null)
		{
			convertView = inflater.inflate(mListLayout, parent, false);
		}
		
		LinearLayout list_contents=(LinearLayout) convertView.findViewById(R.id.SettingContent_lout);
		
		final int sel_item=position;
		final int requestCode=1234;
		
		list_contents.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{				
				Intent address_list=new Intent(mAct, AddressListActivity.class);
				
				address_list.putExtra("sel_item", sel_item);
				mAct.startActivityForResult(address_list, requestCode);
			}
		});
		
		TextView tv_num = (TextView) convertView.findViewById(R.id.Setting_Contents_tv_num);
		TextView tv_name = (TextView) convertView.findViewById(R.id.Setting_Contents_tv_name);
		TextView tv_type = (TextView) convertView.findViewById(R.id.Setting_Contents_tv_type);
		
		tv_num.setText(position+1+"번");
		
		
		if(mSave_data[position][1].equals("null"))
		{
			tv_name.setText("설정하려면");
			tv_type.setText("클릭!!!");
		}
		else
		{
			tv_name.setText(mSave_data[position][0]+"");
			tv_type.setText(mSave_data[position][2]+"");
		}
		
		
		return convertView;
	}
	
}
